/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.swixml.adapter;

import java.awt.Container;

import nemos.swing.IComponentFactory;

import org.swixml.Factory;
import org.swixml.dom.Attribute;
import org.swixml.factory.BeanFactory;
import org.w3c.dom.Element;

/**
 * FactoryAdapter : The SwiXML adapter for OSGi component factories.
 * 
 * <p>
 * This class adapts {@link IComponentFactory} service into a SwiXML
 * {@link Factory}.
 * </p>
 * 
 * @author Romain DUBOIS
 */
public class FactoryAdapter extends BeanFactory {

    /** The adapted factory */
    private final IComponentFactory mAdapted;

    /**
     * Constructor.
     * 
     * @param pAdapted
     *            The component factory to adapt
     */
    public FactoryAdapter(final IComponentFactory pAdapted) {
        super(pAdapted.getType());

        mAdapted = pAdapted;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.factory.BeanFactory#create(java.lang.Object,
     *      org.w3c.dom.Element)
     */
    @Override
    public Object create(@SuppressWarnings("unused") final Object owner,
            @SuppressWarnings("unused") final Element element) {
        return mAdapted.createComponent();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.Factory#newInstance(java.lang.Object[])
     */
    @Override
    public Object newInstance(@SuppressWarnings("unused") final Object... parameter) {
        // ATTR_INITCLASS has been deprecated
        throw new UnsupportedOperationException();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.factory.BeanFactory#getPropertyType(java.lang.Object,
     *      java.lang.String)
     */
    @Override
    public Class<?>[] getPropertyType(final Object bean, final String name) {
        if (mAdapted.handleDynamicProperties()) {
            final Class<?> lType = mAdapted.getPropertyType(name);
            if (lType == null) {
                return null;
            }
            return new Class<?>[] { lType };
        }

        return super.getPropertyType(bean, name);
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.factory.BeanFactory#setProperty(java.lang.Object,
     *      org.swixml.dom.Attribute, java.lang.Object, java.lang.Class)
     */
    @Override
    public void setProperty(final Object bean, final Attribute attr, final Object value,
            final Class<?> type) throws Exception {
        if (mAdapted.handleDynamicProperties()) {
            mAdapted.setProperty(attr.getLocalName(), value, (Container) bean);
        } else {
            super.setProperty(bean, attr, value, type);
        }
    }
}
